package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.custom.Vo.CategoryVo;
import com.future.hair.server.entity.Category;
import com.future.hair.server.entity.ReserveCategory;
import com.future.hair.server.mapper.CategoryMapper;
import com.future.hair.server.mapper.ReserveCategoryMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("//hair/categoryVo")
@Api(tags = "CategoryVo管理接口")
public class CategoryVoController {

    @Autowired
    private CategoryMapper mapper;

    @Autowired
    private ReserveCategoryMapper reserveCategoryMapper;


    @ApiOperation(value = "查询 reserveList 列表")
    @RequestMapping(value = "/reserveList", method = RequestMethod.POST)
    public CommonResult<List<CategoryVo>> reserveList() {
        List<CategoryVo> reserveList = new ArrayList<CategoryVo>();
        List<Category> categoryList = categoryList(1);
        for (Category info :
                categoryList) {
            reserveList.add(getCategoryVo(info));
        }
        return CommonResult.data(reserveList);
    }

    @ApiOperation(value = "查询 worksList 列表")
    @RequestMapping(value = "/worksList", method = RequestMethod.POST)
    public CommonResult<List<Category>> worksList() {
        return CommonResult.data(categoryList(2));
    }

    @ApiOperation(value = "查询当前 CategoryVo")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<CategoryVo> categoryVoId(@RequestBody Category data) {
        return CommonResult.data(getCategoryVo(mapper.selectById(data.getId())));
    }

    public List<Category> categoryList(Integer type) {
        QueryWrapper<Category> categoryWrapper = new QueryWrapper<Category>();
        categoryWrapper.eq("type", type);
        return mapper.selectList(categoryWrapper);
    }

    public CategoryVo getCategoryVo(Category data){
        if (null==data){
            return null;
        }
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setName(data.getName());
        categoryVo.setId(data.getId());
        categoryVo.setType(data.getType());
        categoryVo.setIsEdit(data.getIsEdit());
        QueryWrapper<ReserveCategory> reserveCategoryWrapper = new QueryWrapper<ReserveCategory>();
        reserveCategoryWrapper.eq("category_id", data.getId());
        categoryVo.setChildren(reserveCategoryMapper.selectList(reserveCategoryWrapper));
        return categoryVo;
    }
}
